#include "PCH.h"
#include "..\Common.h"

Dialog::Dialog()
{
	WindowHandle = 0;
	hInstance = 0;
	ResourceId = 0;
}

Dialog::Dialog(HINSTANCE hAppInstance, int DlgResourceId)
{
	hInstance = hAppInstance;
	ResourceId = DlgResourceId;
}

HWND Dialog::MakeDialog(HWND parent)
{
	return CreateDialogParam(hInstance, MAKEINTRESOURCE(ResourceId), parent, Dialog::MessageRouter, (LPARAM)this);
}

BOOL WINAPI Dialog::MessageRouter(HWND WindowHandle, UINT WindowMessage, WPARAM wParam, LPARAM lParam)
{
	Dialog *Self;

	if(WindowMessage == WM_INITDIALOG) {
		Self = reinterpret_cast<Dialog*>(lParam);
		Self->WindowHandle = WindowHandle;

		SetWindowLongPtr(WindowHandle, GWLP_USERDATA, reinterpret_cast<LPARAM>(Self));

	} else {
		Self = reinterpret_cast<Dialog*>(GetWindowLongPtr(WindowHandle, GWLP_USERDATA));
	}

	return (Self) ? Self->HandleMessage(WindowMessage, wParam, lParam) : FALSE;
}